#多线程
import requests
from lxml import etree
import threading
import  time
def craw(start):
    url = 'https://yz.chsi.com.cn/sch/search.do?'
    param = {'start': start}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'
    }
    response = requests.get(url, params=param, headers=headers)
    data_text = response.text
    tree = etree.HTML(data_text)
    div_list = tree.xpath('//div[@class="sch-list-container"]/div')
    for div in div_list:
        name_data = div.xpath('.//a[@class="name js-yxk-yxmc text-decoration-none"]/text()')[0]
        name_data = name_data.strip()
        href_data = 'https://yz.chsi.com.cn' + div.xpath('.//a[@class="name js-yxk-yxmc text-decoration-none"]/@href')[0]
        print(name_data, href_data)
    print(f'------{int(start) / 20 + 1}------')
# 创建一个线程列表来存储线程对象
threads = []
start_range = 0
end_range = 800
# 创建并启动线程
stat = time.time()
for start in range(start_range, end_range, 20):
    thread = threading.Thread(target=craw, args=(str(start),))
    threads.append(thread)
    thread.start()
for thread in threads:
    thread.join()
end = time.time()
if __name__ == '__main__':

    craw(start_range)

    print("multi_thread cost", end - stat)

